---
category: String
created: '2021-05-28'
title: Remove empty lines of a text document
updated: '2021-10-13'
---

**JavaScript version**

```js removeEmptyLines.js
const removeEmptyLines = (str) =>
    str
        .split(/\r?\n/)
        .filter((line) => line.trim() !== '')
        .join('\n');
```

**TypeScript version**

```ts removeEmptyLines.ts
const removeEmptyLines = (str: string): string =>
    str
        .split(/\r?\n/)
        .filter((line) => line.trim() !== '')
        .join('\n');
```

**Examples**

```js examples.js
removeEmptyLines(`red

green
blue

yellow`);

/* Output */
/*
red
green
blue
yellow
*/
```
